package gold.contest;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5995 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        /**
         * Created by fanzhenyu02 on 2020/6/27.
         * Union Find 算法实现
         */
        public class UF {
            // 连通分量个数
            private int count;
            // 存储一棵树
            private int[] parent;
            // 记录树的“重量”
            private int[] size;

            public UF(int n) {
                this.count = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            /* 将 p 和 q 连接 */
            public void union(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                if (rootP == rootQ)
                    return;

                // 小树接到大树下面，较平衡
                if (size[rootP] > size[rootQ]) {
                    parent[rootQ] = rootP;
                    size[rootP] += size[rootQ];
                } else {
                    parent[rootP] = rootQ;
                    size[rootQ] += size[rootP];
                }
                count--;
            }

            /* 判断 p 和 q 是否连通 */
            public boolean connected(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                return rootP == rootQ;
            }

            private int find(int x) {
                while (parent[x] != x) {
                    // 进行路径压缩
                    parent[x] = parent[parent[x]];
                    x = parent[x];
                }
                return x;
            }

            /* 返回图中有多少个连通分量 */
            public int count() {
                return count;
            }

            /* 返回图中联通集合 */
            public Map<Integer, List<Integer>> outputAggregateSet(int n) {
                Map<Integer, List<Integer>> aggregateMap = new HashMap<>();

                // 共有n个集合
                for (int i = 0; i < n; i++) {
                    int parentId = find(i);
                    if (!aggregateMap.containsKey(parentId)) aggregateMap.put(parentId, new ArrayList<>());
                    aggregateMap.get(parentId).add(i);
                }

                System.out.println(aggregateMap.toString());
                return aggregateMap;
            }
        }

        public int[] groupStrings(String[] words) {
            UF uf = new UF(words.length);
            Map<String, List<Integer>> idxMap = new HashMap<>();

            for (int i = 0; i < words.length; i++) {
                if (!idxMap.containsKey(words[i])) idxMap.put(words[i], new ArrayList<>());
                idxMap.get(words[i]).add(i);
            }

            // 往 s1 的字母集合中添加一个字母。
            // 从 s1 的字母集合中删去一个字母。
            // 将 s1 中的一个字母替换成另外任意一个字母（也可以替换为这个字母本身）
            for (int i = 0; i < words.length; i++) {
                // 1. 在j删除
                for (int j = 0; j < words[i].length(); j++) {
                    String curChange = words[i].substring(0, j) + words[i].substring(j + 1, words[i].length());
                    if (idxMap.containsKey(curChange)) {
                        joinConnection(idxMap.get(curChange), uf, i);
                    }
                }

                for (char ch = 'a'; ch <= 'z'; ch++) {
                    // 2. 添加 ch
                    for (int j = 0; j < words[i].length(); j++) {
                        String curChange = words[i].substring(0, j) + ch + words[i].substring(j, words[i].length());
                        if (idxMap.containsKey(curChange)) {
                            joinConnection(idxMap.get(curChange), uf, i);
                        }
                    }
                    if (idxMap.containsKey(words[i] + ch)) {
                        joinConnection(idxMap.get(words[i] + ch), uf, i);
                    }

                    // 3. 替换 ch
                    for (int j = 0; j < words[i].length(); j++) {
                        String curChange = words[i].substring(0, j) + ch + words[i].substring(j + 1, words[i].length());
                        if (idxMap.containsKey(curChange)) {
                            joinConnection(idxMap.get(curChange), uf, i);
                        }
                    }
                }
            }

            int[] ans = new int[2];
            ans[0] = uf.count;
            Map<Integer, Integer> cntMap = new HashMap<>();
            for (int i = 0; i < words.length; i++) {
                cntMap.put(uf.parent[i], cntMap.getOrDefault(uf.parent[i], 0) + 1);
            }
            int maxCnt = 0;
            for (Integer cnt : cntMap.values()) {
                if (cnt > maxCnt) maxCnt = cnt;
            }
            ans[1] = maxCnt;
            return ans;
        }

        public void joinConnection(List<Integer> idxList, UF uf, int curIdx) {
            if (null == idxList || idxList.size() <= 0) return;
            for (Integer target : idxList) {
                uf.union(curIdx, target);
            }
        }
    }

    public void run() {
        Solution solution = new Solution();
//        System.out.println(Arrays.toString(solution.groupStrings(new String[]{"a", "b", "ab", "cde"})));
//        System.out.println(Arrays.toString(solution.groupStrings(new String[]{"a", "ab", "abc"})));
        System.out.println(Arrays.toString(solution.groupStrings(new String[]{"ghnv", "uip", "tenv", "hvepx", "e", "ktc", "byjdt", "ulm", "cae", "ea"})));
    }

    public static void main(String[] args) throws Exception {
        LC5995 an = new LC5995();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
